package cn.edu.xhu.library_management.book.controller;

import cn.edu.xhu.library_management.book.entity.LmBorrow;
import cn.edu.xhu.library_management.book.entity.LmReservation;
import cn.edu.xhu.library_management.book.entity.dto.BookRecordDTO;
import cn.edu.xhu.library_management.book.service.LmBorrowService;
import cn.edu.xhu.library_management.book.utils.AccountUtils;
import cn.edu.xhu.library_management.common.entity.R;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: MMJ
 * @Date: 2023 - 04 - 19
 * @Description: 图书借阅
 */
@Api(tags = "图书借阅")
@ApiSort(4)
@RestController
@RequestMapping("/lm_book/borrow")
public class BookBorrow {
    @Autowired
    private LmBorrowService borrowService;
    @Value("${minio.server}")
    private String imgServer;
    @Value("${minio.port}")
    private int serverPort;
    @PostMapping
    @ApiOperation(value = "借阅图书", produces = "application/json")
    @ApiOperationSupport(order = 421)
    public boolean borrowBook(@RequestBody LmBorrow borrow){
        borrow.setBoStatus("1");
        borrow.setBoDelFlag("1");
        borrow.setUserType(AccountUtils.getAccountType(borrow.getUserAccount()));
        return borrowService.save(borrow);
    }

    @GetMapping("/re/{account}")
    @ApiOperation(value = "查询用户已归还借阅记录", produces = "application/json")
    @ApiOperationSupport(order = 422)
    public List<BookRecordDTO> reBorrow(@PathVariable("account") String account){
        List<BookRecordDTO> books = borrowService.reBorrow(account);
        books.forEach(book -> book.setBoImage(imgServer + ":" + serverPort + book.getBoImage()));
        return books;
    }

    @GetMapping("/noRe/{account}")
    @ApiOperation(value = "查询用户未归还借阅记录", produces = "application/json")
    @ApiOperationSupport(order = 423)
    public List<BookRecordDTO> noReBorrow(@PathVariable("account") String account){
        List<BookRecordDTO> books = borrowService.noReBorrow(account);
        books.forEach(book -> book.setBoImage(imgServer + ":" + serverPort + book.getBoImage()));
        return books;
    }
}
